package com.ruoyi.business.communication.listener.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.business.communication.domain.SlotStatus;
import com.ruoyi.business.communication.listener.DeviceStatusListener;
import com.ruoyi.business.device.domain.Device;
import com.ruoyi.common.core.redis.RedisCache;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Redis设备状态监听器实现
 * 将设备状态数据保存到Redis中
 * 
 * @author ruoyi
 */
@Component
public class RelayDeviceStatusListener implements DeviceStatusListener {
    
    private static final Logger logger = LoggerFactory.getLogger(RelayDeviceStatusListener.class);
    
    // Redis Key前缀 
    // 设备槽位状态缓存前缀 使用：DeviceSlotStatus:设备中继号 relayCode
    private static final String DEVICE_SLOT_STATUS_PREFIX = "DeviceSlotStatus[RC]:";
    
    @Autowired
    private RedisCache redisCache;
    
    @Override
    public void onStatusUpdate(Device device, List<SlotStatus> slotStatusList) {
        try {
            if (device == null || slotStatusList == null || slotStatusList.isEmpty()) {
                logger.warn("设备或槽位状态数据为空，跳过保存");
                return;
            }
            
            String deviceRelayCode = device.getDeviceRelayCode();
            if (deviceRelayCode == null || deviceRelayCode.isEmpty()) {
                logger.warn("设备中继号为空，跳过保存");
                return;
            }
            
            // 保存槽位状态列表到Redis
            String slotStatusKey = DEVICE_SLOT_STATUS_PREFIX + deviceRelayCode;
            redisCache.setCacheObject(slotStatusKey, slotStatusList);
            
            // 设置过期时间（可选，这里设置为1小时）
            //redisCache.expire(slotStatusKey, 3600);
            
            logger.debug("设备状态已保存到Redis - 设备: {}, 槽位数量: {}", 
                deviceRelayCode, slotStatusList.size());
                
        } catch (Exception e) {
            logger.error("保存设备状态到Redis时发生异常: {}", e.getMessage(), e);
        }
    }
    
    
    /**
     * 从Redis获取设备槽位状态
     * 
     * @param deviceRelayCode 设备中继号
     * @return 槽位状态列表
     */
    public List<SlotStatus> getDeviceSlotStatus(String deviceRelayCode) {
        try {
            String slotStatusKey = DEVICE_SLOT_STATUS_PREFIX + deviceRelayCode;
            return redisCache.getCacheObject(slotStatusKey);
        } catch (Exception e) {
            logger.error("从Redis获取设备槽位状态时发生异常: {}", e.getMessage(), e);
            return null;
        }
    }
    
    /**
     * 清除设备状态缓存
     * 
     * @param deviceRelayCode 设备中继号
     */
    public void clearDeviceStatus(String deviceRelayCode) {
        try {
            String slotStatusKey = DEVICE_SLOT_STATUS_PREFIX + deviceRelayCode;
            
            redisCache.deleteObject(slotStatusKey);
            
            logger.debug("已清除设备状态缓存 - 设备: {}", deviceRelayCode);
        } catch (Exception e) {
            logger.error("清除设备状态缓存时发生异常: {}", e.getMessage(), e);
        }
    }
}
